<template>
  <div class="user-wrapper">
    <div class="content-box">
      <a-icon v-if="companyName" type="home" style="margin-right: 10px; font-size: 20px;" />
      <!-- <a-select :default-value="currCompId" :value="value" v-if="myTeam.length > 1" @change="changCompany">
        <a-select-option v-for="item in myTeam" :key="item.compId">
          {{ item.companyName }}
        </a-select-option>
      </a-select> -->
      <span>{{ companyName }}</span>
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <a-avatar class="avatar" size="default" icon="user" :src="avatar"/>
          <span>{{ nickname }}</span>
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="2">
            <a href="javascript:;" @click="handlechangePwd">
              <a-icon type="edit"/>
              <span>修改密码</span>
            </a>
          </a-menu-item>
          <a-menu-item key="3">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout"/>
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </div>
    <chang-pwd-dialog ref="changPwdDialog" @ok="hangePwd"/>
  </div>
</template>

<script>
import changPwdDialog from '@/components/commonDialog/changPwdDialog'
import { mapActions, mapGetters } from 'vuex'
export default {
  name: 'UserMenu',
  components: {
    changPwdDialog
  },
  data () {
    return {
      value: ''
    }
  },
  mounted () {
    this.value = this.currCompId
  },
  computed: {
    ...mapGetters(['nickname', 'avatar', 'companyName', 'currCompId', 'myTeam'])
  },
  methods: {
    ...mapActions(['Logout', 'switchComp']),

    // 修改密码
    handlechangePwd () {
      this.$refs.changPwdDialog.show()
    },

    hangePwd () {
      return this.Logout({}).then(() => {
        setTimeout(() => {
          window.location.reload()
        }, 16)
      }).catch(err => {
        this.$message.error({
          title: '错误',
          description: err.message
        })
      })
    },

    handleLogout () {
      return this.Logout({}).then(() => {
        setTimeout(() => {
          window.location.reload()
        }, 16)
      })
    },
    changCompany (value) {
      var id = this.currCompId
      this.$store.commit('SET_CURRCOMPID', value)
      return this.switchComp(value)
        .then(res => {
          if (res.code === 0) {
            this.value = value
            window.location.reload()
          } else {
            this.value = id
            this.$message.error(res.msg)
          }
        })
    }
  }
}
</script>
<style scoped>
  .content-box{
    display: flex;
    align-items: center;
  }
</style>
